package com.wu.flandre.configuration;

import com.wu.flandre.component.interceptor.JwtTokenHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义MVC配置类
 *
 * @author 50816
 * @date 2019年12月1日 19:06:49
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    /**
     * 配置跨域访问
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("GET", "POST", "PUT", "DELETE","OPTIONS")
                .allowedHeaders("*")
                .allowedOrigins("*")
                .maxAge(3600);
    }

    /**
     * 注册jwt token验证拦截器
     */
    @Bean
    public JwtTokenHandlerInterceptor jwtTokenHandlerInterceptor() {
        return new JwtTokenHandlerInterceptor();
    }

    /**
     * 添加token拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> noInterceptors = new ArrayList<>();
        noInterceptors.add("/test");
        noInterceptors.add("/swagger-resources/**");
        noInterceptors.add("/webjars/**");
        noInterceptors.add("/v2/**");
        noInterceptors.add("/swagger-ui.html/**");
//        registry.addInterceptor(jwtTokenHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns(noInterceptors);
    }
}
